# cython: language_level=3
import logging
import os

import coloredlogs

logger_dict = {}


def get_logger(filename: str = "server"):
    if not os.path.exists("logs"):
        os.makedirs("logs")
    if logger_dict.get(filename):
        return logger_dict.get(filename)
    logger = logging.getLogger(filename)
    logger.setLevel(logging.DEBUG)
    fh = logging.FileHandler(filename=f'logs/{filename}.log')
    formatter = logging.Formatter(
        "%(asctime)s - %(module)s - %(funcName)s - line:%(lineno)d - %(levelname)s - %(message)s"
    )
    coloredlogs.install(level='DEBUG', logger=logger,
                        fmt="%(asctime)s - %(module)s - %(funcName)s - line:%(lineno)d - %(levelname)s - %(message)s")
    # ch = logging.StreamHandler()
    # ch.setFormatter(formatter)
    fh.setFormatter(formatter)
    # logger.addHandler(ch)  # 将日志输出至屏幕
    logger.addHandler(fh)  # 将日志输出至文件
    logger_dict[filename] = logger
    return logger
